package com.ys.controller.web.finance;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.finance.TcwWanliCurrencyExchange;
import com.ys.entity.finance.TcwWanliCurrencyExchangeItem;
import com.ys.service.finance.TcwWanliCurrencyExchangeService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/web/finance/tcwWanliCurrencyExchange")
public class WebTcwWanliCurrencyExchangeController {


	@Autowired
	private CommonApiService<TcwWanliCurrencyExchange> commonApiService;

	@Autowired
	private TcwWanliCurrencyExchangeService tcwWanliCurrencyExchangeService;


	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_WanliCurrencyExchange);
			TcwWanliCurrencyExchange tcwWanliCurrencyExchange = new TcwWanliCurrencyExchange()
					.setStatus(1)
					.setBid(bid)
					.setCreateTime(new Date());

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliCurrencyExchange, new ArrayList<Map<String, Object>>());

			commonApiService.toAdd(model, tcwWanliCurrencyExchange, MenuId.TCW_WanliCurrencyExchange, null, subGridConfig);
			return "finance/tcwWanliCurrencyExchange/tcwWanliCurrencyExchangeAdd";
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			if (params.get("bid") == null){
				return W.error(model, "单号不能为空");
			}

			String bid = (params.get("bid").toString());

			Map entity = tcwWanliCurrencyExchangeService.getExtraParam(bid);
			if (entity == null){
				return W.error(model, "货币兑换单不存在!");
			}
			List<Map<String, Object>> sonlist = tcwWanliCurrencyExchangeService.getItemList(bid);

			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_WanliCurrencyExchange, sonlist);

			commonApiService.toEdit(model, Long.valueOf(entity.get("id").toString()), new TcwWanliCurrencyExchange(), MenuId.TCW_WanliCurrencyExchange, entity, subGridConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "finance/tcwWanliCurrencyExchange/tcwWanliCurrencyExchangeEdit";
	}

}
